package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.BannerBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopListBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.ShopTypeBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IShopView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/21.
 */

public class ShopPresenter extends BasePresenter<IShopView> {

    public ShopPresenter(IShopView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void getBanner() {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getBanner("3")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<BannerBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<BannerBean>> response) {
                        iView.setBanner(response.getData());
                        getGoodsList("1");
                    }
                }));
    }

    public void getShopType(String id, String level) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).getShopType(id, level)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<ShopTypeBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<ShopTypeBean> response) {
                        iView.setShopType(response.getData().getType());
                    }
                }));
    }

    public void getGoodsList(String page) {
        Map<String, String> map = new HashMap<>();
        map.put("page", page);
        map.put("size", "10");
        addSubscription(RetrofitSource.createApi(HttpPostService.class).shop_list(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<ShopListBean>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<ShopListBean>> response) {
                        iView.setGoodsList(response.getData());
                    }
                }));
    }

}
